package PlistMaker;

use Exporter;

@ISA = qw(Exporter);
@EXPORT_OK = qw( p_if p_ifnot CopyPath RemovePath AbortOperation AddSource Exec InstallApp UninstallApp LinkPath MovePath Notice RemoveSource SetStatus IsFirmwareVersion IsPackageInstalled ExistsPath Confirm ExecNoError CreatePackage );
%EXPORT_TAGS = (all => [ qw( p_if p_ifnot CopyPath RemovePath AbortOperation AddSource Exec InstallApp UninstallApp LinkPath MovePath Notice RemoveSource SetStatus IsFirmwareVersion IsPackageInstalled ExistsPath Confirm ExecNoError CreatePackage ) ]);

sub p_if($$) {
    my ($test, $commands)  = @_;

    return p_all("If", $test, $commands);
}

sub p_ifnot($$) {
    my ($test, $commands)  = @_;
    
    return p_all("IfNot", $test, $commands);
}

sub p_all($$$) {
    my ($type, $test, $commands)  = @_;

    chomp $test;
    chomp $commands;
    $test =~ s/^/      /mg;
    $commands =~ s/^/      /mg;

    my $phrase = <<END
<array>
  <string>$type</string>
  <array>
${test}
  </array>
  <array>
${commands}
  </array>
</array>
END
    ;
    chomp $phrase;
    return $phrase;
}


sub callFn {
    # From the XML doc pov, the fn name is just another parameter

    my $params = "<string>" . join("</string>\n<string>", @_) . "</string>";

    $params =~ s/^/  /mg;

    my $phrase = <<END;
<array>
$params
</array>
END
    ;
    #chomp $phrase;
    return $phrase;
}

# CopyPath(source,dest)
sub CopyPath($$) {
    return callFn("CopyPath", @_);
}

# RemovePath(dest)
sub RemovePath($) {
    return callFn("RemovePath", @_);
}

# AbortOperation(message)
sub AbortOperation($) {
    return callFn("AbortOperation", @_);
}

# AddSource(url)
sub AddSource($) {
    return callFn("AddSource", @_);
}

# Exec(command)
sub Exec($) {
    return callFn("Exec", @_);
}

# InstallApp(application name)
sub InstallApp($) {
    return callFn("InstallApp", @_);
}

# UninstallApp(application name)
sub UninstallApp($) {
    return callFn("UninstallApp", @_);
}

# LinkPath(oldPath, newPath)
sub LinkPath($$) {
    return callFn("LinkPath", @_);
}

# MovePath(oldPath, newPath)
sub MovePath($$) {
    return callFn("MovePath", @_);
}

# Notice(string)
sub Notice($) {
    return callFn("Notice", @_);
}

# RemoveSource(url)
sub RemoveSource($) {
    return callFn("RemoveSource", @_);
}

# SetStatus(string)
sub SetStatus($) {
    return callFn("SetStatus", @_);
}

# FirmwareVersionIs(string)
sub IsFirmwareVersion {
    # This one is special...  The parameter list is an array.

    my $params = "<array>\n  <string>" . join("</string>\n  <string>", @_) . "</string>\n</array>";

    $params =~ s/^/  /mg;

    my $phrase = <<END;
<array>
  <string>FirmwareVersionIs</string>
$params
</array>
END
;
    chomp $phrase;
    return $phrase;
}

# InstalledPackage(string)
sub IsPackageInstalled($) {
    return callFn("InstalledPackage", @_);
}

# ExistsPath(path)
sub ExistsPath($) {
    return callFn("ExistsPath", @_);
}

# Confirm(text, option1, option2)
sub Confirm($$$) {
    return callFn("Confirm", @_);
}

# Dunno, exec command, ignoring errors?
sub ExecNoError($) {
    return callFn("ExecNoError", @_);
}

#my %Descriptor = ( bundleIdentifier => "ca.pollock.fixEbookDir", 
#		   name => "EBook /mobile fix",
#		   version => "1.0",
#		   location => "",
#		   category => "Tweaks", 
#		   size => "0",
#		   hash => "",
#		   url => "http://jason-pollock.blogspot.com/",
#		   maintainer => "Jason Pollock",
#		   contact => "jason@pollock.ca",
#		   description => "Move existing eBooks from /var/root to /var/mobile.  Create a link from /var/root to /var/mobile.",
#		   install => $fix_disk_install,
#		   preflight => $fix_dist_,
#		   postflight => "" ,
#                  uninstall => "",
#                  update => "");


# Create the package dict entry, from the provided hash.
sub CreatePackage {
    my %dict = @_;
    
    foreach my $i (install, uninstall, update, preflight, postflight) {
	if (exists $dict{$i}) {
	    $dict{$i} =~ s/^/  /mg;
	    $dict{$i} =<<END 	    
<key>$i</key>
<array>
$dict{$i}
</array>
END
      ; 
	    $dict{$i} =~ s/^/         /mg;
	    chomp $dict{$i};
	}
    }

    my $phrase = <<END
    <dict>
       <key>bundleIdentifier</key>
       <string>$dict{bundleIdentifier}</string>
       <key>name</key>
       <string>$dict{name}</string>
       <key>version</key>
       <string>$dict{version}</string>
       <key>location</key>
       <string>$dict{location}</string>
       <key>category</key>
       <string>$dict{category}</string>
       <key>size</key>
       <string>$dict{size}</string>
       <key>hash</key>
       <string>$dict{hash}</string>
       <key>url</key>
       <string>$dict{url}</string>
       <key>maintainer</key>
       <string>$dict{maintainer}</string>
       <key>contact</key>
       <string>$dict{contact}</string>
       <key>description</key>
       <string>$dict{description}</string>
       <key>scripts</key>
       <dict>
$dict{preflight}	 
$dict{install}
$dict{update}
$dict{uninstall}
$dict{postflight}
       </dict>
     </dict>
END
;

    return $phrase;
}


1;
